using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Ssb
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SsbPC : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        /// <summary>
        /// currentStage contains references to the fighters in the stage and all underlying information
        /// </summary>
        public Stage currentStage;

        /// <summary>
        /// currentMenu holds information about the MenuItems in it which in turn link to other menus.
        /// Also, contains a reference to the parent menu.
        /// </summary>
        public Menu currentMenu;

        public SsbPC()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            // set to 800x600
            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth = 800;

            graphics.SynchronizeWithVerticalRetrace = true; // wait for vsync

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.


            Services.AddService(typeof(SpriteBatch), new SpriteBatch(GraphicsDevice)); // Add the spritebatch to the services
            Services.AddService(typeof(ContentManager), Content); // Add the ContentManager to the services

            currentMenu = (Menu)Content.Load<ContentTypes.Menu_Content>("Menus\\Menus\\SplashScreen").Build(this);
            currentStage = new Stage(this, new Rectangle(0, 0, 1, 1), new List<Logic.Bubble>(), new List<Rectangle>(), null);
            currentStage.Enabled = false;

            Components.Add(currentMenu);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            currentMenu.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            this.GraphicsDevice.Clear(Color.White);

            ((SpriteBatch)this.Services.GetService(typeof(SpriteBatch))).Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.SaveState);

            if(currentMenu.Enabled)
                currentMenu.Draw(gameTime);

            if (currentStage.Enabled)
                currentStage.Draw();

            base.Draw(gameTime);

            ((SpriteBatch)this.Services.GetService(typeof(SpriteBatch))).End();
        }
    }
}
